package cz.muni.fi.pa165.mushroom.website.servicesImpl;


import cz.muni.fi.pa165.mushroom.website.dao.GathererDao;
import cz.muni.fi.pa165.mushroom.website.dto.GathererDto;
import cz.muni.fi.pa165.mushroom.website.entities.Gatherer;
import cz.muni.fi.pa165.mushroom.website.services.GathererService;
import cz.muni.fi.pa165.mushroom.website.transformerAdapter.Transformer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresRoles;

/**
 * @author: Veronika Lovrantova
 */

@Service
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
public class GathererServiceImpl implements GathererService {

    @Autowired
    private GathererDao gathererDao;

    public GathererDao getGathererDao() {
        return gathererDao;
    }

    public void setGathererDao(GathererDao gathererDao) {
        this.gathererDao = gathererDao;
    }
    
    @Override    
    @Transactional
    public void createGatherer(GathererDto gathererDto) {
        gathererDao.createGatherer(Transformer.transformGathererDtoOnGatherer(gathererDto));
    }

    @Override
    @RequiresRoles("ROLE_USER")
    @Transactional
    public void updateGatherer(GathererDto gathererDto) {
        gathererDao.updateGatherer(Transformer.transformGathererDtoOnGatherer(gathererDto));
    }

    @Override
    @RequiresRoles("ROLE_ADMIN")
    @Transactional
    public void removeGatherer(GathererDto gathererDto) {
        gathererDao.removeGatherer(Transformer.transformGathererDtoOnGatherer(gathererDto));
    }

    @Override
    @RequiresRoles("ROLE_USER")
    @Transactional
    public GathererDto findGatherer(Long id) {
        return Transformer.transformGathererOnGathererDto(gathererDao.findGatherer(id));
    }

    @Override
    @Transactional
    public List<GathererDto> listGatherers() {
        List<GathererDto> dtos = new ArrayList<>();
        for(Gatherer gatherer : gathererDao.listGatherers()) {
            dtos.add(Transformer.transformGathererOnGathererDto(gatherer));
        }
        return dtos;
    }    
    
    @Override
    @RequiresRoles("ROLE_USER")
    @Transactional
    public GathererDto findGathererByUsername(String username){
        return Transformer.transformGathererOnGathererDto(gathererDao.findGathererByUsername(username));
    }
    
}
